<%@page import="com.milk_shop.util.SendSmsUtil"%>
<%@page import="java.util.List"%>
<%@page import="com.opensymphony.xwork2.ActionContext"%>
<%@page import="com.milk_shop.model.User"%>
<%@taglib uri="/struts-tags" prefix="s" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% 
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath %>"/>
<%List<User> users = (List<User>)ActionContext.getContext().get("users"); %>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>四季奶坊店员管理界面</title>
  <script src="js/vue.js"></script>
  <link rel="stylesheet" href="https://unpkg.com/element-ui@2.14.1/lib/theme-chalk/index.css">
  <script src="https://unpkg.com/element-ui@2.14.1/lib/index.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
  <style>
    .nav-name {
      margin: 10px auto;
    }

    .manage-btn {
      text-align: center;
      margin-right: 100px;
      font-size: 12px
    }

    .el-table .warning-row {
      background: oldlace;
    }

    .el-table .success-row {
      background: #f0f9eb;
    }

    .account-container {
      width: 100%;
      margin: 0 25px;
    }

    .tb {
      width: 800px;
      margin: 50px auto;
    }
  </style>
</head>

<body>
  <div id="app">
    <el-container style="height: 650px;">

      <div class="account-container">
        <div class="nav-name">
          <h2>账号管理</h2>
        </div>
        <div class="manage-btn">
          <el-button type="primary" @click="addDialogVisible = true" plain>添加</el-button>
        </div>
        <div class="tb">
          <el-table :data="tableData" style="width: 100%" :row-class-name="tableRowClassName"
            element-loading-text="Loading" @cell-dblclick="tableEdit">
            <el-table-column label="账号">
              <template slot-scope="scope">{{ scope.row.username }}</template>
            </el-table-column>
            <el-table-column prop="name" label="姓名">
              <template slot-scope="scope">
                <el-input v-model="scope.row.name"></el-input>
              </template>
            </el-table-column>
            <el-table-column prop="authorization" label="权限">
              <template slot-scope="scope">
                <el-select v-model="scope.row.authorization" filterable allow-create default-first-option placeholder="请选择权限">
                  <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                  </el-option>
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center">
              <template slot-scope="scope">
                <el-button type='danger' @click="deleteData(scope.row.username)">删除</el-button>
                <el-button type='primary' @click="updateData(scope.row)">更新</el-button>
              </template>
            </el-table-column>
          </el-table>

          <el-dialog title="添加(添加后，默认密码为账号)" :visible.sync="addDialogVisible">
            <el-form :model="form">
              <el-form-item label="姓名" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="手机号" :label-width="formLabelWidth">
                <el-input v-model="form.phone" style="width: 60%" autocomplete="off"></el-input>
                <el-button plain type="primary" @click="send(form.phone)">发送验证码</el-button>
              </el-form-item>
              <el-form-item label="验证码" :label-width="formLabelWidth">
                <el-input v-model="form.captcha" style="width: 30%"></el-input>
              </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
              <el-button @click="addDialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="addData(form.phone,form.captcha,form.name)">确 定</el-button>
            </div>
          </el-dialog>

          <el-dialog title="删除" :visible.sync="deleteDialogVisible">
            <el-table :data="tableData">
              <el-table-column prop="username" label="用户名">
              </el-table-column>
              <el-table-column prop="name" label="用户名">
              </el-table-column>
              <el-table-column label="删除">
                <template>
                  <el-button type="danger" native-type="submit" @click="deleteData" icon="el-icon-delete" circle>
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-dialog>
        </div>
    </el-container>
  </div>
  <script>
    new Vue({
      el: "#app",
      data: {
        addDialogVisible: false,
        deleteDialogVisible: false,
        options: [{
          value: '<%=User.IDENTIFY_ADMIN %>',
          label: '管理员'
        }, {
          value: '<%=User.IDENTIFY_OWNER %>',
          label: '店员'
        }],
        tableData: [
        <%for(int i = 0;i < users.size();i++){
        		  out.print("{");
        		  out.print("username:\""+users.get(i).getPhone()+"\",");
        		  out.print("name:\""+users.get(i).getName()+"\",");
        		  out.print("authorization:\""+users.get(i).getRole()+"\"");
        		  out.print("},");
        	  } %>
        ],
        form: {
          username: '',
          name: '',
        },
        formLabelWidth: '120px'
      },
      methods: {
        deleteData:function(phone){
    		this.$message({
    			type: 'submit',
    			message: '删除成功'
    		});
        	window.location.href="/MilkShop/account_manage/deleteUser?phone="+phone;
        },
        updateData:function(row){
        	if(row.name === ''){
        		this.$message({
        			type: 'warning',
        			message: '输入姓名不能为空：错误'
        		});
        		return
        	}
    		this.$message({
    			type: 'submit',
    			message: '更新成功'
    		});
        	window.location.href="/MilkShop/account_manage/updateUser?phone="+row.username+"&name="+row.name+"&role="+row.authorization;
        },
        addData:function(phone,captcha,name){
        	if(phone === '' || name === '' || captcha === '' || phone === undefined || name === undefined || captcha === undefined){
        		this.$message({
        			type: 'warning',
        			message: '输入信息不能为空'
        		});
        		return
        	};
        	if(!captcha === '1234'){
        		this.$message({
        			type: 'error',
        			message: '手机号验证：错误'
        		});
        		return
        	};
    		this.$message({
    			type: 'submit',
    			message: '添加成功'
    		});
        	window.location.href="/MilkShop/account_manage/addUser?user.phone="+phone+"&vertifyCode="+captcha+"&user.name="+name;
        },
        send:function(phone){
    		this.$message({
    			type: 'info',
    			message: '验证码已发送(:1234)'
    		});
        	//TODO:验证码调用
        }
      },
    })
  </script>
</body>

</html>